package cn.bs.seckill.service.impl;

import cn.bs.seckill.pojo.SeckillOrders;
import cn.bs.seckill.mapper.SeckillOrdersMapper;
import cn.bs.seckill.pojo.User;
import cn.bs.seckill.service.SeckillOrdersService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 布索
 * @since 2022-03-01
 */
@Service
public class SeckillOrdersServiceImpl extends ServiceImpl<SeckillOrdersMapper, SeckillOrders> implements SeckillOrdersService {

    @Autowired
    private SeckillOrdersMapper seckillOrderMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    //获取秒杀结果
    @Override
    public Long getResult(User user, Long goodsId) {
        SeckillOrders seckillOrder = seckillOrderMapper.selectOne(new QueryWrapper<SeckillOrders>().eq("user_id", user.getId()).eq("goods_id", goodsId));
        if (null != seckillOrder) {
            return seckillOrder.getOrderId();
        } else {
            if (redisTemplate.hasKey("isStockEmpty:" + goodsId)) {
                return -1L;
            } else {
                return 0L;
            }
        }
    }
}
